#include "ApplicationSettings.h"
#include "qsettings.h"


/** @class ApplicationSettings
 *
 * @brief This class holds all settings to be reloaded on next program launch.
 *
 * It is made up of static getter and setter methods, to retrieve and store
 * needed settings. Stored info is:
 * - full path of last show
 * - default volume
 * - selected language
 */

const QString COMPANY_TAG = QString("Me.Te.Or.");
const QString PRODUCT_TAG = QString("Starvax Player");
const int NUMBER_OF_RECENT_SHOW_FILES = 5;


void ApplicationSettings::addRecentShowFile(const QString &filePath)
{
   QSettings setting( COMPANY_TAG, PRODUCT_TAG);

   /* load list of last shows and remove the older, if needed */
   QStringList fileSet = setting.value( "recent show files", QStringList()).toStringList();

   fileSet.removeAll(filePath);
   fileSet.prepend(filePath);

   while (fileSet.size() >= NUMBER_OF_RECENT_SHOW_FILES)
   {
      /* remove the oldest inserted file  */
      fileSet.removeLast();
   }

   setting.setValue( "recent show files", fileSet);
   emit recentShowsChanged();
}

void ApplicationSettings::removeRecentShowFile(const QString & filePath)
{
   QSettings setting( COMPANY_TAG, PRODUCT_TAG);

   /* load list of last shows and remove the older, if needed */
   QStringList fileSet = setting.value( "recent show files", QStringList()).toStringList();

   fileSet.removeAll(filePath);

   setting.setValue( "recent show files", fileSet);
   emit recentShowsChanged();
}

QStringList ApplicationSettings::recentShowFiles() const
{
   QSettings setting( COMPANY_TAG, PRODUCT_TAG);
   QStringList fileSet = setting.value( "recent show files", QStringList()).toStringList();

   return fileSet;
}

int ApplicationSettings::maxRecentShowFiles() const
{
   return NUMBER_OF_RECENT_SHOW_FILES;
}

void ApplicationSettings::setDefaultVolume(int volume)
{
   QSettings setting( COMPANY_TAG, PRODUCT_TAG);
   setting.setValue( "default volume", volume);
}

int ApplicationSettings::defaultVolume() const
{
   QSettings setting( COMPANY_TAG, PRODUCT_TAG);
   int defVolume = setting.value( "default volume", 100).toInt();

   return defVolume;
}

void ApplicationSettings::setLanguage(ApplicationSettings::Language language)
{
   QSettings setting( COMPANY_TAG, PRODUCT_TAG);
   setting.setValue( "language", language);
}

ApplicationSettings::Language ApplicationSettings::language() const
{
   QSettings setting( COMPANY_TAG, PRODUCT_TAG);
   Language language = (Language)(setting.value( "language", NATIVE).toInt());

   return language;
}

void ApplicationSettings::saveWindowSetting(const QByteArray windowSetting)
{
   QSettings setting( COMPANY_TAG, PRODUCT_TAG);
   setting.setValue( "window state", windowSetting);
}

QByteArray ApplicationSettings::getPreviousWindowSetting() const
{
   QSettings setting( COMPANY_TAG, PRODUCT_TAG);
   QByteArray windowSetting = setting.value( "window state").toByteArray();

   return windowSetting;
}

void ApplicationSettings::saveWindowGeometry(const QByteArray windowGeometry)
{
   QSettings setting( COMPANY_TAG, PRODUCT_TAG);
   setting.setValue( "window geometry", windowGeometry);
}

QByteArray ApplicationSettings::getPreviousWindowGeometry() const
{
   QSettings setting( COMPANY_TAG, PRODUCT_TAG);
   QByteArray windowGeometry = setting.value( "window geometry").toByteArray();

   return windowGeometry;
}


void ApplicationSettings::setOpenMusicFolder(const QString & folder)
{
   QSettings setting( COMPANY_TAG, PRODUCT_TAG);
   setting.setValue( "open music folder", folder);
}

QString ApplicationSettings::openMusicFolder() const
{
   QSettings setting( COMPANY_TAG, PRODUCT_TAG);
   QString musicFolder = setting.value( "open music folder").toString();

   return musicFolder;
}

void ApplicationSettings::setOpenScriptFolder(const QString & folder)
{
   QSettings setting( COMPANY_TAG, PRODUCT_TAG);
   setting.setValue( "open script folder", folder);
}

QString ApplicationSettings::openScriptFolder() const
{
   QSettings setting( COMPANY_TAG, PRODUCT_TAG);
   QString musicFolder = setting.value( "open script folder").toString();

   return musicFolder;
}
